import datetime
from serial_operate import stand_frozen_data
from tools import deal_time, get_previous_15min_time, get_all_meter_addr, modify_centos_current_date_restart, \
    execute_sql
from stand_requests import request_refresh_cache

com_name = 'com4'
ip = "192.168.253.113"

# 设置站端时间为PC当前时间
modify_centos_current_date_restart(ip)
execute_sql(ip, "UPDATE sys_config SET config_value=0 WHERE config_key='sys.ratioErrorStatus';")
request_refresh_cache(ip, 8082, "oetsky", "oetsky@45296")
execute_sql(ip, "TRUNCATE err_voltage_error;")
execute_sql(ip, "TRUNCATE da_voltage_data_temp;")

start, end = get_previous_15min_time()
list_meter_addr = get_all_meter_addr(ip)

start_time = deal_time(start)
end_time = deal_time(end)

transformer_address, collect_time, frequency, amplitude, phase, ratio, angle, status, _, _ = stand_frozen_data(
    start_time, end_time, list_meter_addr[0], com_name)
print(transformer_address, collect_time, frequency, amplitude, phase, ratio, angle, status)

# 【检查点1】 采集数据和计算数据全为0，采样数据状态字为类型4，内容=0110
assert frequency == 0
assert amplitude == 0
assert phase == 0
assert ratio == 0
assert angle == 0
assert status == 15
assert (datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S") - datetime.datetime.strptime(
    collect_time, "%Y-%m-%d %H:%M:%S")).total_seconds() == 4500.0

# 系统设置通过比差值上传评估状态码参数为1
execute_sql(ip, "UPDATE sys_config SET config_value=1 WHERE config_key='sys.ratioErrorStatus';")
request_refresh_cache(ip, 8082, "oetsky", "oetsky@45296")
transformer_address, collect_time, frequency, amplitude, phase, ratio, angle, status, _, _ = stand_frozen_data(
    start_time, end_time, list_meter_addr[0], com_name)
print(transformer_address, collect_time, frequency, amplitude, phase, ratio, angle, status)
# 【检查点1】 采集数据和计算数据全为0，采样数据状态字为类型4，内容=0110
assert frequency == 0
assert amplitude == 0
assert phase == 0
assert ratio == 1150000
assert angle == 1150000
assert status == 15
